package selfview.sufutian.com.myapplication.draw2d;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Created by sufutian on 2017/7/25.
 */

public class DrawAPIView extends View {

    private Paint paint;

    public DrawAPIView(Context context) {
        super(context);
        initPaint();
    }

    public DrawAPIView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public DrawAPIView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    public void initPaint() {

    }


    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);

        paint = new Paint();
        paint.setColor(Color.RED);// 设置红色
        paint.setAntiAlias(true);
        paint.setTextSize(50);

        canvas.drawText("画点:", 0, 50, paint);
        canvas.drawPoint(120, 25, paint);
        canvas.drawPoint(130, 25, paint);

        canvas.drawText("画线:", 140, 50, paint);
        canvas.drawLine(240, 30, 500, 40, paint);

        canvas.drawText("画矩形", 550, 50, paint);
        canvas.drawRect(700, 0, 1000, 100, paint);

        paint.setColor(Color.BLUE);
        canvas.drawText("画椭圆,圆:", 0, 150, paint);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawOval(300, 100, 500, 200, paint);
        }
        canvas.drawCircle(700, 150, 50, paint);

        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);//设置空心
        canvas.drawText("画弧形,扇形:", 0, 250, paint);
        RectF oval = new RectF(300, 200, 600, 300);
        canvas.drawArc(oval, 250, 250, false, paint);//开始的角度 扫过的角度
        Shader mShader = new LinearGradient(0, 0, 100, 100,
                new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.LTGRAY},
                null, Shader.TileMode.REPEAT); // 一个材质,打造出一个线性梯度沿著一条线。
        paint.setShader(mShader);
        paint.setStyle(Paint.Style.FILL);//设置空心
        oval.set(700, 200, 1000, 400);
        Paint paint2 = new Paint();
        paint2.setColor(Color.GRAY);
        canvas.drawRect(700, 200, 1000, 400, paint2);
        canvas.drawArc(oval, 250, 120, true, this.paint);//开始的角度  扫过的角度

        this.paint.setColor(Color.LTGRAY);
        canvas.drawText("多边形:", 0, 400, this.paint);
        Path path = new Path();
        path.moveTo(200, 370);
        path.lineTo(250, 300);
        path.lineTo(400, 300);
        path.lineTo(450, 370);
        path.lineTo(400, 440);
        path.lineTo(250, 440);
        path.lineTo(200, 370);
        path.close(); // 使这些点构成封闭的多边形
        canvas.drawPath(path, this.paint);


        canvas.drawText("贝塞尔曲线:", 0, 500, this.paint);
        this.paint.reset();
        this.paint.setStyle(Paint.Style.STROKE);
        this.paint.setColor(Color.RED);
        this.paint.setShader(null);
        Path path2 = new Path();
        path2.moveTo(300, 480);//设置Path的起点
        path2.quadTo(400, 480, 470, 410); //设置贝塞尔曲线的控制点坐标和终点坐标
        canvas.drawPath(path2, this.paint);//画出贝塞尔曲线


        final Paint paint1 = new Paint();
        paint1.setAntiAlias(true);
        paint1.setTextSize(20);
        paint1.setColor(Color.BLUE);
        paint1.setStrokeWidth(10);
        paint1.setStyle(Paint.Style.STROKE);
//        canvas.drawRect(0, 600, 800, 1400, paint1);
        canvas.drawCircle(400, 1000, 400, paint1);


        Paint citePaint = new Paint();
        citePaint.setStyle(Paint.Style.FILL);
        citePaint.setTextSize(44);
        citePaint.setStrokeWidth(2);
        citePaint.setAntiAlias(true);
        citePaint.setColor(Color.BLUE);

        Path path3 = new Path();
        path3.addArc(new RectF(200, 800, 600, 1200), 180, 180);
        canvas.drawTextOnPath("明月几时有?把酒问青天,我欲乘风", path3, 14, 0, citePaint);

        Paint tmpPaint = new Paint(paint); //小刻度画笔对象tmpPaint
        tmpPaint.setColor(Color.RED);
        tmpPaint.setTextSize(40);
        tmpPaint.setAntiAlias(true);
        tmpPaint.setStrokeWidth(10);

        canvas.save();
        canvas.translate(400, 1000);//画布移动到圆心
        for (int i = 0; i < 60; i++) {
            if (i % 15 == 0) {
                paint1.setStrokeWidth(20);
                canvas.drawLine(0, 330, 0, 400, paint1);
            } else {
                paint1.setStrokeWidth(15);
                canvas.drawLine(0, 360, 0, 400, paint1);
            }
            canvas.rotate(6);
        }

        canvas.restore();
        paint1.setStyle(Paint.Style.FILL);
        canvas.drawCircle(400, 1000, 40, paint1);
        paint1.setColor(Color.RED);
        canvas.drawCircle(400, 1000, 20, paint1);
        canvas.save();


        canvas.translate(400, 1000);//画布移动到圆心
        canvas.rotate(6 * a);
        canvas.drawRect(-5, 80, 5, -380, paint1);



    }

    int a = 1;


    public void moveView() {

        handler.sendEmptyMessage(0);
    }

    Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            a++;
            invalidate();
//            postInvalidate();//可以在非UI线程调用
            handler.sendEmptyMessageDelayed(0, 1000);
        }
    };


    float downX, downY, moveX, moveY, upX, upY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = event.getX();
                moveY = event.getY();
                actionMove();
                break;
            case MotionEvent.ACTION_UP:
                upX = event.getX();
                upY = event.getY();
                actionUp();
                break;
        }
        return true;
    }


    float offsetX, offsetY;

    public void actionMove() {
        offsetX = moveX - downX;
        offsetY = moveY - downY;
        Log.e("sufutian==", "actionMove: offsetX: " + offsetX + " offsetY: " + offsetY);

//        scrollTo(-(int) offsetX, -(int) offsetY);

        /**
         * layout中心防止位置
         */
        layout((int) (getLeft()+offsetX), (int) (getTop()+offsetY), (int) (getRight()+offsetX), (int) (getBottom()+offsetY));

    }


    public void actionUp() {
        scrollTo(0, 0);
    }
}

